<template>
  <el-dialog
    :close-on-click-modal="false"
    :show-close="false"
    :title="isAdd?'提现':'充值'"
    :visible.sync="isEdit"
  >
    <el-form
      :rules="rules"
      ref="ruleForm"
      :model="form"
      label-width="120px"
      size="small"
      class="form-box"
    >
      <el-form-item v-if="isAdd" label="提现金额" prop="amount">
        <el-input type="number" v-model="form.amount" placeholder="请输入提现金额"></el-input>
      </el-form-item>
      <el-form-item v-else label="充值金额" prop="amount">
        <el-input type="number" v-model="form.amount" placeholder="请输入充值金额"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.remark" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('closeDialog')" size="small">取消</el-button>
      <el-button @click="submitFormAdd('ruleForm')" type="primary" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script lang="ts">
import { Component, Vue, Prop, Provide } from "vue-property-decorator";
import storageApi from "@/api/storage";
@Component({
  components: {}
})
export default class typeEditDialog extends Vue {
  // 属性或参数中使用 ！：表示强制解析（告诉typescript编译器，这里一定有值），常用于vue-decorator中的@Prop
  check:boolean = true//防重复提交
  @Prop(Number) id!: number
  @Prop(String) amounts!: string
  @Prop(Boolean) isEdit!: boolean;
  @Prop(Boolean) isAdd!: boolean;
  @Prop(Array) projectNames!: [];
  @Provide() dateTime: any = [];
  @Prop(Object) form!: {
    mch_id:string,
    type: string,
    amount: string,
    remark:string,
  };
  @Provide() rules: any = {
    amount: [{
      //pattern: /^[1-9]\d*(\.\d+)?$/,  //大于1
      pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/,  //正则
      message: '请输入大于0的数'
    }],
  };
  // 提交
  submitFormAdd(formName: any) {
    (this.$refs[formName] as any).validate((valid: boolean) => {
      if (valid) {
        if(this.checkAge(this.form.amount)) {
          if(this.check) {
            let _this = this
            _this.check = false
             storageApi.saveRecharge(_this.form)
            .then((res: any) => {
            if(res.data.code==200){
                _this.$message({
                  message: res.data.msg,
                  type: "success"
                });
                _this.$emit("closeDialog");
                _this.$emit("loadData")
                setTimeout(function(){
                  _this.check = true
                },3000)
              }else{
                _this.$message({
                  message: res.data.msg,
                  type: "error"
                });
                _this.check = true
              }
            });
          }
         
        }
      }
    })
  }
  checkAge(value) {
    console.log(this.id)
    if(this.id ==1) {
      if (parseInt(value) > parseInt(this.amounts)) {
        this.$message({
          message: '输入最大限值'+this.amounts,
          type: "error"
        });
        return false
      }
      return true
    }
    return true
  }
  created() {
  }
}
</script>

<style lang="scss" scoped>
.form-box {
  .el-input,
  .el-select {
    width: 200px !important;
  }
}
</style>
